import {reqAddOrUpdateCart, reqDetailList} from '@/api/index.js';

// 材料：
let state = {
    detailInfo: {}
};

// 加工：
let getters = {
    // 面包屑的数据
    categoryView(state) {
        return state.detailInfo.categoryView || {};
    },
    // 商品信息的数据
    skuInfo() {
        return state.detailInfo.skuInfo || {};
    },
    // 商品销售属性列表的数据
    spuSaleAttrList() {
        return state.detailInfo.spuSaleAttrList || [];
    }
};

// 厨师：
let mutations = {
    DETAILINFO(state, value) {
        state.detailInfo = value;
    }
};

// 迎宾：
let actions = {
    async getDetailInfo({state, commit, dispatch}, skuId) {
        console.log('请求参数', skuId);
        let result = await reqDetailList(skuId);
        console.log('响应数据', result.data);
        if (result.code === 200) {
            commit('DETAILINFO', result.data);
        }
    },
    async addOrUpdateCart({state, commit, dispatch}, {skuId, skuNum}) {
        let result = await reqAddOrUpdateCart(skuId, skuNum);
        if (result.code === 200) {
            return result;
        } else {
            return Promise.reject();
        }
    }
};

// 对外暴露
export default {
    state,
    mutations,
    actions,
    getters
};



